/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class RetryPanel extends Composite {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context;

	private SimplePanel panel;

	private DialogBox box;

	private boolean loaded;

	private CheckBox useNewData;

	private TextArea data;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				String msg = "";
				if (code.equals("-1")) {
					msg = cons.retryDataError();
				} else {
					msg = res.getMsg();
				}
				Window.alert("There was an error: " + msg);
				// box.hide();
				return;
			}

			Window.alert(cons.retrySend());
			box.hide();

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RetryPanel(StatMessages cons1, BifangGWTServiceAsync service1,
			GwtContext ctx, DialogBox box1) {
		this.service = service1;
		this.cons = cons1;
		this.box = box1;
		this.context = ctx;
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setSpacing(5);

		panel = new SimplePanel();
		panel.setWidth("100%");
		vp.add(panel);
		// gIds = new HashMap<String, String>();
		//		
		// for(Usergroup ug : gs){
		// gIds.put(ug.getId(), "");
		// }
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(5);

		Button retry = new Button(cons.retry());
		retry.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {

				RequestObject req = new RequestObject();
				req.setAction("han.bifang.gwt.server.action.stat.RetryAction");
				if (useNewData.isChecked()) {
					if(StringUtils.isEmpty(data.getText())){
						Window.alert(cons.dateFormatError());
						return;
					}

					
						req.setRequest(data.getText());
					
				}
				// req.setRequest("error");
				service.call(req, cb);

			}
		});
		hp.add(retry);
		Button re = new Button(cons.close());
		re.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				box.hide();

			}
		});
		hp.add(re);
		vp.add(hp);

		initWidget(vp);

	}

	public void onLoad() {
		if (loaded)
			return;

		Grid table = new Grid(2, 1);
		table.setWidth("100%");

		useNewData = new CheckBox(cons.useNewData());
		useNewData.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				data.setEnabled(true);

			}
		});

		table.setWidget(0, 0, useNewData);
		data = new TextArea();
		data.setVisibleLines(9);
		data.setCharacterWidth(60);
		data.setEnabled(false);
		table.setWidget(1, 0, data);

		panel.setWidget(table);
		loaded = true;
	}

}
